package com.fengwk.support.util;

import java.io.File;
import java.io.IOException;

import com.fengwk.support.exception.BaseException;
import com.fengwk.support.util.LoggerUtils.Logger;

/**
 * 文件工具
 */
public class FileUtils {
	
	private static final Logger LOG = LoggerUtils.getLogger(FileUtils.class);
	
	private FileUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	/**
	 * 是否存在
	 * @param pathname
	 * @return
	 */
	public static boolean exists(String path) {
		File file = new File(path);
		return file.exists();
	}
	
	/**
	 * 存在断言
	 * 
	 * @param path
	 * @return
	 */
	public static void assertExists(File file) {
		if (file == null)
			throw new BaseException("file == null");
		if (!file.exists())
			throw new BaseException("!file.exists()");
	}
	
	/**
	 * 授权
	 * 
	 * @param file
	 */
	public static void auth(File file) {
		file.setWritable(true);
		file.setReadable(true);
	}
	
	/**
	 * 根据文件夹路径创建文件夹路径上所有的文件夹
	 * @param directoryPath
	 */
	public static void createDirectoryPath(String dirPath) {
		createDirectoryPath(buildFile(dirPath));
	}
	
	public static void createDirectoryPath(File dirFile) {
		if(dirFile.exists()) {
			if (dirFile.isDirectory())
				return;
			else
				delete(dirFile);
		}
		if(!dirFile.mkdirs()) {
		    LOG.error("error creating folder path");
		}
	}
	
	/**
	 * 列出文件夹所有子文件
	 * @param file
	 * @return 存在子文件返回File[],不存在返回null
	 */
	public static File[] listFiles(File file) {
		if(file.isDirectory()) {
			File[] files = file.listFiles();
			if(ArrayUtils.isNotBlank(files)) {
				return files;
			}
		}
		return null;
	}
	
	/**
	 * 列出文件夹所有子文件
	 * @param filePath
	 * @return 存在子文件返回File[],不存在返回null
	 */
	public static File[] listFiles(String filePath) {
		return listFiles(new File(filePath));
	}
	
	/**
	 * 构建File
	 * 
	 * @param path
	 * @param isMkdirs 是否自动构建该路径
	 * @return
	 */
	public static File buildFile(String path, boolean isMkdirs) {
		if(StringUtils.isBlank(path)) {
			return null;
		}
		File file = new File(path);
		File pFile = file.getParentFile();
		
		// 构建路径
		if(isMkdirs && pFile != null && !pFile.exists())
			pFile.mkdirs();
			
		// 权限注入
		auth(file);
		
		return file;
	}
	
	/**
	 * 构建File
	 * 
	 * @param path
	 * @return
	 */
	public static File buildFile(String path) {
		return buildFile(path, false);
	}
	
	/**
	 * 文件删除
	 * 
	 * @param path 文件路径
	 */
	public static void delete(String path) {
		delete(buildFile(path));
	}
	
	/**
	 * 文件删除
	 * 
	 * @param file
	 * @param deleteSelf false时不删除文件本身
	 */
	public static void delete(File file, boolean deleteSelf) {
		if(file != null && file.exists()) {
			if (file.isDirectory()) {
				File[] fs = file.listFiles();
				if (ArrayUtils.isNotBlank(fs)) {
					for (File f: fs)
						delete(f);
				}
			}
			if (deleteSelf)
				file.delete();
		}
	}
	
	/**
	 * 文件删除,将会删除文件本身
	 * 
	 * @param file 文件File
	 */
	public static void delete(File file) {
		if(file != null && file.exists()) {
			if (file.isDirectory()) {
				File[] fs = file.listFiles();
				if (ArrayUtils.isNotBlank(fs)) {
					for (File f: fs)
						delete(f);
				}
			}
			file.delete();
		}
	}
	
	/**
	 * 移动
	 * 
	 * @param srcPath
	 * @param tarPath
	 * @throws IOException
	 */
	public static void move(String srcPath, String tarPath) throws IOException {
		File srcFile = buildFile(srcPath);
		File tarFile = buildFile(tarPath);
		copy(srcFile, tarFile);
		delete(srcFile);
	}
	
	/**
	 * 移动
	 * 
	 * @param srcFile
	 * @param tarPath
	 * @throws IOException
	 */
	public static void move(File srcFile, String tarPath) throws IOException {
		File tarFile = buildFile(tarPath);
		copy(srcFile, tarFile);
		delete(srcFile);
	}
	
	/**
	 * 移动
	 * 
	 * @param srcPath
	 * @param tarFile
	 * @throws IOException
	 */
	public static void move(String srcPath, File tarFile) throws IOException {
		File srcFile = buildFile(srcPath);
		copy(srcFile, tarFile);
		delete(srcFile);
	}
	
	/**
	 * 移动
	 * 
	 * @param srcFile
	 * @param tarFile
	 * @throws IOException
	 */
	public static void move(File srcFile, File tarFile) throws IOException {
		copy(srcFile, tarFile);
		delete(srcFile);
	}
	
	/**
	 * 拷贝
	 * 
	 * @param srcPath
	 * @param tarPath
	 * @throws IOException
	 */
	public static void copy(String srcPath, String tarPath) throws IOException {
		File srcFile = buildFile(srcPath);
		File tarFile = buildFile(tarPath);
		copy(srcFile, tarFile);
	}
	
	/**
	 * 拷贝
	 * 
	 * @param srcPath
	 * @param tarFile
	 * @throws IOException
	 */
	public static void copy(String srcPath, File tarFile) throws IOException {
		File srcFile = buildFile(srcPath);
		copy(srcFile, tarFile);
	}
	
	/**
	 * 拷贝
	 * 
	 * @param srcFile
	 * @param tarPath
	 * @throws IOException
	 */
	public static void copy(File srcFile, String tarPath) throws IOException {
		File tarFile = buildFile(tarPath);
		copy(srcFile, tarFile);
	}
	
	/**
	 * 拷贝
	 * 
	 * @param srcFile
	 * @param tarFile
	 * @throws IOException
	 */
	public static void copy(File srcFile, File tarFile) throws IOException {
		IoUtils.write(IoUtils.buildInputStream(srcFile), tarFile);
	}
	
}
